package com.example.myapplication2;

import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.myapplication2.Receiver.SystemReceiver;

public class SystemBroadcastActivity extends AppCompatActivity implements View.OnClickListener {

    private SystemReceiver systemReceiver;
    private SystemReceiver networkReceiver;
    private SystemReceiver alarmSystemReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_broadcast);
        findViewById(R.id.AlarmSystemReceiverSend).setOnClickListener(this);
        findViewById(R.id.AlarmSystemReceiverCancel).setOnClickListener(this);
        findViewById(R.id.AlarmSystemReceiverLoop).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.AlarmSystemReceiverSend:
                alarmSystemReceiver.sendAlarm();
                System.out.println("发送定时任务");
                break;
            case R.id.AlarmSystemReceiverCancel:
                alarmSystemReceiver.cancel();
                System.out.println("取消定时任务");
                break;
            case R.id.AlarmSystemReceiverLoop:
                alarmSystemReceiver.setLoop();
                System.out.println("设置循环");
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        //系统时间
        systemReceiver = new SystemReceiver();
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_TIME_TICK);
        registerReceiver(systemReceiver,intentFilter);
        //网络广播注册
        networkReceiver = new SystemReceiver();
        IntentFilter intentFilterNetwork = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
        registerReceiver(networkReceiver,intentFilterNetwork);

        //定时管理器,此处使用getApplicationContext(),保证内存安全
        alarmSystemReceiver = new SystemReceiver(getApplicationContext());
        IntentFilter intentFilterAlarm=new IntentFilter("SystemAlarm");
        registerReceiver(alarmSystemReceiver,intentFilterAlarm);
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(systemReceiver);
        unregisterReceiver(networkReceiver);
        unregisterReceiver(alarmSystemReceiver);
    }

}